//
//  LBPageListSubSegmentController.m
//  TEXT
//
//  Created by mac on 2024/8/24.
//  Copyright © 2024 刘博. All rights reserved.
//

#import "LBPageListSubSegmentController.h"
#import "LBFunctionTestHeader.h"
#import "LBPageSmoothView.h"
#import "LBSingleCell.h"
#import "SPPageMenu.h"
#import "LBCollectionView.h"
#import "LBFLowLayout.h"
#import "LBPageListSubSingleViewController.h"

@interface LBPageListSubSegmentController ()<LBPageSmoothListViewDelegate,
UICollectionViewDelegate,
UICollectionViewDataSource,
UIScrollViewDelegate,
LBPageListSubSingleViewControllerDelegate,
SPPageMenuDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) LBPageListSubSingleViewController *fist;
@property (nonatomic, strong) LBPageListSubSingleViewController *second;

@property (nonatomic, assign) NSInteger currentIndex;
 
@property (nonatomic, strong) SPPageMenu *menu;

@property (nonatomic, strong) NSMutableArray *collectionViewsArray;

@property (nonatomic, weak) NSObject *observer;

@property (nonatomic, assign) BOOL isClickingSegment;

@end

@implementation LBPageListSubSegmentController

- (instancetype)init
{
    self = [super init];
    if (self) {
//        [self.collectionView addObserver:self forKeyPath:@"contentInset" options:NSKeyValueObservingOptionNew context:nil];
//        [self.collectionView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.scrollView];
    self.currentIndex = 1;
    [self.view addSubview:self.menu];
    [self.menu setSelectedItemIndex:self.currentIndex];
    [self.collectionViewsArray addObject:self.fist.collectionView];
    [self.collectionViewsArray addObject:self.second.collectionView];
//    [self.collectionView reloadData];
//    self.currentCollectionView = ((LBSingleCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]).collectionView;
    // Do any additional setup after loading the view.
}

#pragma mark - lazy load

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LBSingleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LBSingleCell" forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor blueColor];
    //[cell.contentView addSubview:self.scrollView];
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 1;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(containerRemoveObserverForListView:)]) {
        [self.delegate containerRemoveObserverForListView:self.listScrollView];
    }
    NSInteger currentIndex = (scrollView.contentOffset.x + 1)/scrollView.bounds.size.width;
    self.currentIndex = currentIndex;
    if (self.delegate && [self.delegate respondsToSelector:@selector(containerScrollViewDidEndDecelerating:)]) {
        [self.delegate containerScrollViewDidEndDecelerating:scrollView];
    }
    [self.menu setSelectedItemIndex:currentIndex];
}

- (BOOL)hasSubSegment
{
    return YES;
}

- (UIView *)listView
{
    return self.view;
}

- (UIScrollView *)listScrollView
{
    if (self.currentIndex == 0) {
        return self.fist.collectionView;
    }
    return self.second.collectionView;
}

- (BOOL)listScrollViewShouldReset
{
    return NO;
}

- (void)updateOtherSublistScrollViewContentWithCurrentListScroll:(UIScrollView *)scrollView contentInset:(UIEdgeInsets)contentInset
{
    [self.collectionViewsArray enumerateObjectsUsingBlock:^(LBObserverCollectionview *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj != scrollView) {
            obj.contentInset = contentInset;
        }
    }];
}

- (void)updateOtherSublistScrollViewContentOffsetWithCurrentListScroll:(UIScrollView *)scrollView contentOffset:(CGPoint)contentOffset
{
    [self.collectionViewsArray enumerateObjectsUsingBlock:^(LBObserverCollectionview *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj != scrollView) {
            [obj setContentOffset:contentOffset animated:NO];
        }
    }];
}

- (void)configScrollsTopWithScrollView:(UIScrollView *)scrollView
{
    [self.collectionViewsArray enumerateObjectsUsingBlock:^(UICollectionView *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.scrollsToTop = (obj == scrollView);
    }];
}

#pragma mark - scrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.isClickingSegment) {
        return;
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(containerScrollViewDidScroll:)]) {
        [self.delegate containerScrollViewDidScroll:scrollView];
    }
    [self.menu beginMoveTrackerFollowScrollView:scrollView];
}

#pragma mark - LBPageListSubSingleViewControllerDelegate

- (void)listScrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y < - 340 - subSegmentHeight) {
        self.menu.frame = CGRectMake(0, - scrollView.contentOffset.y - subSegmentHeight, ScreenWidth, 40);
    } else {
        if (scrollView.contentOffset.y > - 124 - subSegmentHeight) {
            if (self.menu.zf_y != 124) {
                self.menu.frame = CGRectMake(0, 124, ScreenWidth, 40);
            }
        } else {
            CGFloat y = scrollView.contentOffset.y;
            self.menu.frame = CGRectMake(0, -y - subSegmentHeight, ScreenWidth, 40);
        }
    }
}

#pragma mark - SPPageMenuDelegate

- (void)pageMenu:(SPPageMenu *)pageMenu itemSelectedAtIndex:(NSInteger)index
{
    if (self.isClickingSegment) {
        return;
    }
    self.isClickingSegment = YES;
    if (self.delegate && [self.delegate respondsToSelector:@selector(containerRemoveObserverForListView:)]) {
        [self.delegate containerRemoveObserverForListView:self.listScrollView];
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(containerScrollViewDidScroll:)]) {
        [self.delegate containerScrollViewDidScroll:nil];
    }
    [self.scrollView setContentOffset:CGPointMake(ScreenWidth * index, 0) animated:YES];
    NSInteger currentIndex = index;
    self.currentIndex = currentIndex;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (self.delegate && [self.delegate respondsToSelector:@selector(containerScrollViewDidEndDecelerating:)]) {
            [self.delegate containerScrollViewDidEndDecelerating:nil];
            self.isClickingSegment = NO;
        }
    });
}

#pragma mark - lazy load

- (LBCollectionView *)collectionView
{
    if (!_collectionView) {
        LBFLowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) collectionViewLayout:layout];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        [_collectionView registerClass:[LBSingleCell class] forCellWithReuseIdentifier:@"LBSingleCell"];
        _collectionView.backgroundColor = [UIColor redColor];
      //  [_collectionView addSubview:self.scrollView];
       // _collectionView.contentInset = UIEdgeInsetsMake(0, 0, - (300 - 124), 0);
    }
    return _collectionView;
}

- (SPPageMenu *)menu
{
    if (!_menu) {
        PageViewConfiguration *config = [[PageViewConfiguration alloc] init];
        config.titleArray = @[@"首页", @"推荐"];
        SPPageMenu *menu = [[SPPageMenu alloc] initWithFrame:CGRectMake(0, 340, ScreenWidth, 40) config:config];
        [menu setItems:@[@"视频", @"图片"] selectedItemIndex:self.currentIndex];
        menu.frame = CGRectMake(0, 340, SCREEN_WIDTH, 40);
        menu.delegate = self;
        menu.backgroundColor = [UIColor greenColor];
        _menu = menu;
    }
    return _menu;
}

- (UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        _scrollView.backgroundColor = [UIColor purpleColor];
        _scrollView.contentSize = CGSizeMake(ScreenWidth * 2, ScreenHeight);
        [_scrollView addSubview:self.fist.collectionView];
        [_scrollView addSubview:self.second.collectionView];
        _scrollView.contentOffset = CGPointMake(ScreenWidth, 0);
        _scrollView.bounces = NO;
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
     }
    return _scrollView;
}

- (LBPageListSubSingleViewController *)fist
{
    if (!_fist) {
        _fist = [[LBPageListSubSingleViewController alloc] init];
        _fist.delegate = self;
    }
    return _fist;
}

- (LBPageListSubSingleViewController *)second
{
    if (!_second) {
        _second = [[LBPageListSubSingleViewController alloc] init];
        _second.collectionView.frame = CGRectMake(ScreenWidth, 0, ScreenWidth, ScreenHeight);
        _second.delegate = self;
    }
    return _second;
}

- (NSMutableArray *)collectionViewsArray
{
    if (!_collectionViewsArray) {
        _collectionViewsArray = [NSMutableArray array];
    }
    return _collectionViewsArray;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
